<?php
namespace App\Http\Controllers\Admin;

use App\Models\Admin\Product;
use App\Models\Admin\Store;
use App\Models\Admin\Ucenter;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Models\Admin\Order;


class OrderController extends CommonController
{

//    private $order = new Order();

    public function __construct()
    {
        parent::__construct();
        $this->checkLogin();
    }

    /**
     * 代购订单列表
     */
    public function purchas_list(Request $request) {
        //示例化模型
        $order   = new Order();
        $store   = new Store();
        $user    = new Ucenter();
        $product = new Product();

        $order_no = '';
        if($request->isMethod('post')) { //搜索
            //获取搜索条件
            $order_no = trim($request->input('order_no'));
            if($order_no) {
                //模糊查询订单编号
                $purchas_data = $order->get_purchas_order_by_order_no($order_no);
            }else {
                //查询代购订单信息
                $purchas_data = $order->get_purchas_order();
            }
        }else {
            //查询代购订单信息
            $purchas_data = $order->get_purchas_order();
        }

        //查询代购订单用户信息
        foreach($purchas_data as $key => $value) {
            //获取用户信息
            $user_info      = $user->get_user_info_by_id($value->node_id);
            //获取用户推荐人信息
            $intr_user_info = $user->get_user_info_by_id($user_info->introducer);

            //查询订单详情信息
            $order_detail   = $order->get_order_detail($value->order_id);

            $purchas_data[$key]->product_name  = '';
            $purchas_data[$key]->product_price = '';

            foreach($order_detail as $k => $val) {
               //保存订单详细（tbuy_order_detail）到订单（tbuy_order）中
               if(!$purchas_data[$key]->product_name) {
                   //直接拼接
                   $purchas_data[$key]->product_name  .= $val->product_name;
                   $purchas_data[$key]->product_price .= $val->price;
               }else {
                   //直接拼接
                   $purchas_data[$key]->product_name  .= '&'.$val->product_name;
                   $purchas_data[$key]->product_price .= '&'.$val->price;
               }
            }

            //保存用户信息和订单详细以及商品信息到代购订单信息中
            $purchas_data[$key]->nodename   = $user_info->nodename;
            $purchas_data[$key]->introducer = $intr_user_info->nodename;
        }

        //模板渲染
        return view('admin.order.purchas_list',['data'=>$purchas_data,'order_no'=>$order_no]);
    }

    /**
     * 订单详情
     */
    public function order_detail(Request $request) {
        //实例化模型
        $store   = new Store();
        $order   = new Order();
        $product = new Product();

        //获取参数
        $post_data  = $request->input();

        //查询代购订单信息
        $data       = $order->get_order_by_id($post_data['id']);

        //查询代购订单详细
        $order_detail = $order->get_order_detail($data->order_id);

        foreach($order_detail as $key => $value) {
            //查询商品属性
            $product_sku = $product->get_product_sku_by_id($value->sku_id);
            //保存商品属性信息到代购订单详细中
            $order_detail[$key]->remarks = $product_sku->remarks;
        }

        //查询收货地址
        $consignee = $order->get_consignee_by_id($data->order_id);

        //解析支付详情
        $data->pay_detail = json_decode($data->pay_type_group, true);

        //保存用户名和推荐人到代购订单信息中
        $node = DB::table('tnet_reginfo')->where('nodeid' , $data->node_id)->first();
        $data->nodename   = $node->nodecode;
        $data->introducer = DB::table('tnet_reginfo')->where('nodeid' , $node->introducer)->value('nodecode');

        //展示模板
        return view('admin.order.order_detail',['data'=>$data,'order_detail'=>$order_detail,'consignee'=>$consignee]);
    }

    /**
     * 全球购订单列表
     */
    public function order_list(Request $request){
        $orderModel = new Order();
        $order_no = $request->input('order_no');
        $order_list = $orderModel->get_order_list($order_no);

        return view('admin.order.order_list',['data'=>$order_list,'order_no'=>$order_no]);
    }

}